package com.ubing.boot.notesforjava.test.juc;

import java.util.concurrent.Semaphore;

/**
 * @ClassName : semaphore_test
 * @Description : semaphore_test
 * @Author : Xxxxx
 * @Date: 2023-03-13 11:33
 */
public class semaphore_test {
    public static void main(String[] args) {
        Semaphore s= new Semaphore(5);//  可作为限流，一次5个线程
        Semaphore s1= new Semaphore(5,false);
        for(int i=0;i<50;i++){
            new Thread(()->{
                try {
                    s.acquire();
                    System.out.println(Thread.currentThread().getName()+"running");
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }finally {
                    s.release();
                }
            },i+"").start();
        }
    }
}
